package com.agent.demo;

import java.util.HashMap;
import java.util.Map;

public class TimeMonitor {
    private static final ThreadLocal<Map<String, Long>> startTime = new ThreadLocal<>();

    public static void start(String methodName) {
        if (startTime.get() == null) {
            startTime.set(new HashMap<>());
        }
        startTime.get().put(methodName, System.currentTimeMillis());
    }

    public static void end(String methodName) {
        long endTime = System.currentTimeMillis();
        if (startTime.get() == null || startTime.get().get(methodName) == null) {
            return;
        }
        long executionTime = endTime - startTime.get().get(methodName);
        System.out.println(">---------" + methodName + ": 执行用时" + executionTime + "ms");
        if (startTime.get().size() == 0) {
            startTime.remove();
        }
    }


}